//
//  ZCAllSortTableViewCell.m
//  VoiceMusic
//
//  Created by dllo on 15/10/20.
//  Copyright © 2015年 zhangdeyu. All rights reserved.
//

#import "ZCAllSortTableViewCell.h"
#import "ZCAllSortModel.h"
#import "ZCAllSortCollectionViewCell.h"
#import "ZCAllSortedViewController.h"
@implementation ZCAllSortTableViewCell

#pragma mark 初始化
- (instancetype) initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createViewDesign];
    }
    return self;
}
#pragma mark 创建视图
- (void)createViewDesign
{
    self.flowLayout = [[UICollectionViewFlowLayout alloc] init];
    self.flowLayout.minimumLineSpacing = 0;
    self.flowLayout.minimumInteritemSpacing = 0;
    //针对item
    self.flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    self.mainCollectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:self.flowLayout];
    self.mainCollectionView.dataSource = self;
    self.mainCollectionView.delegate = self;
    self.mainCollectionView.backgroundColor = [UIColor clearColor];
    [self.contentView addSubview:self.mainCollectionView];
    [self.mainCollectionView registerClass:[ZCAllSortCollectionViewCell class] forCellWithReuseIdentifier:@"reuse1"];
}
#pragma mark 约束尺寸
- (void)layoutSubviews
{
    [super layoutSubviews];
    CGRect rect = [[UIScreen mainScreen] bounds];
    self.flowLayout.itemSize = CGSizeMake((rect.size.width - 20) / 4, 40);
    if (self.mainArr.count / 4 != 1) {
        self.mainCollectionView.frame = CGRectMake(10, 10, rect.size.width - 20, 50 * self.mainArr.count / 4 );
    }else {
        self.mainCollectionView.frame = CGRectMake(10, 10, rect.size.width - 20, 50 * self.mainArr.count / 4  + 40);
    }
}
#pragma mark collectionView实现方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.mainArr.count;
}
#pragma mark cell赋值
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ZCAllSortCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse1" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor clearColor];
    ZCAllSortModel *model = self.mainArr[indexPath.row];
    cell.name.text = model.tag;
    return cell;
}
#pragma mark 点击方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    ZCAllSortModel *model = self.mainArr[indexPath.row];
    // 通知中心传递内容
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:model.tag,@"key",nil];
    // 创建通知中心
    [[NSNotificationCenter defaultCenter] postNotificationName:@"return" object:nil userInfo:dic];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
